/*
 * @lc app=leetcode.cn id=128 lang=java
 *
 * [128] 最长连续序列
 */

// @lc code=start

import java.util.*;

class Solution {
    public static int longestConsecutive(int[] nums) {
        Set<Integer> s = new HashSet();
        for (int i : nums)
            s.add(i);
        int max_len = 0;
        for (int i : nums) {
            if (s.contains(i)) {
                s.remove((Integer) i);
                int left = i - 1;
                while (s.contains(left)) {
                    s.remove((Integer) left);
                    left--;
                }
                int right = i + 1;
                while (s.contains(right)) {
                    s.remove((Integer) right);
                    right++;
                }
                max_len = Math.max(right - left - 1, max_len);
            }
        }
        return max_len;
    }
}
// @lc code=end
